// BattleState.java
package org.zjx.core.state.domain;

import lombok.Data;
import org.zjx.core.controller.TimeoutCapable;
import org.zjx.core.state.StateComponent;

import java.time.Instant;
import java.util.Optional;

/**
 * 战斗领域状态实现
 */
@Data
public class BattleState implements StateComponent, TimeoutCapable {
    private String battleId;
    private long version;
    private Instant startTime;
    private int roundCount;
    private boolean timeout;

    public BattleState(String battleId) {
        this.battleId = battleId;
        this.version = System.nanoTime();
        this.startTime = Instant.now();
    }


    @Override
    public void reset() {
        this.version = System.nanoTime();
        this.roundCount = 0;
        this.timeout = false;
    }


    @Override
    public StateComponent copy() {
        BattleState copy = new BattleState(battleId);
        copy.version = this.version;
        copy.startTime = this.startTime;
        copy.roundCount = this.roundCount;
        copy.timeout = this.timeout;
        return copy;
    }

    @Override
    public boolean isTimeout() {
        return timeout || Instant.now().isAfter(startTime.plusSeconds(300)); // 5分钟超时
    }

    // 业务方法
    public void incrementRound() {
        this.roundCount++;
        this.version = System.nanoTime();
    }

    public void markTimeout() {
        this.timeout = true;
        this.version = System.nanoTime();
    }


}